//
//  FR_StudentAccountViewController.m
//  FaceRecognition
//
//  Created by syun on 17/2/8.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_StudentAccountViewController.h"
#import "FDCalendar.h"

@implementation ParAttendLabel

+ (instancetype)labelWithFrame:(CGRect)frame desText:(NSString *)desText conText:(NSString *)conText{
    
    ParAttendLabel *label = [[ParAttendLabel alloc] init];
    label.desText = desText;
    label.contentText = conText;
    label.frame = frame;
    label.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
    label.font = [UIFont systemFontOfSize:15];
    label.text = [NSString stringWithFormat:@"%@:   %@",desText,conText];
    return label;
}

- (void)setContentText:(NSString *)contentText{
    
    _contentText = contentText;
    
    self.text = [NSString stringWithFormat:@"%@:   %@",self.desText,self.contentText];
}

@end

@interface FR_StudentAccountViewController ()<FDCalendarDelegate,UIPickerViewDataSource,UIPickerViewDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    UIControl *control;
    
    NSString *thisYear;
    NSString *thisMonth;
    
    NSString *selectYearStr;
    
    UILabel      *_navTitlelabel;
    UIImageView  *_navImageView;
    UIImageView  *navBackView;
    
    UIView *line2;
    UIButton *imgBtn;
    
    UILabel *QJLabel;//请假
    UILabel *amLabel;//上午
    UILabel *pmLabel;//下午
    UIView * line3;
    UIView * line4;
    UILabel * KQTJLabel;//考勤统计
    
    NSString * selStudentID;//当前选择的学生ID
    
    NSString *selDateStr;//当前选择的日
    
    NSString * lastMonth;//上次选择的月份
    
    NSInteger  index;//选择第几个孩子
}
@property (nonatomic, strong) UIScrollView *bgScrollView;
@property (nonatomic, strong) UIButton *classBtn;
@property (nonatomic, strong) UIButton *dateBtn;
@property (nonatomic, strong) UIPickerView *classPicker;
@property (nonatomic, strong) UIPickerView *datePicker;
@property (nonatomic, strong) FDCalendar  *calender;

@property (nonatomic, strong) NSDictionary *classInfoDic;
@property (nonatomic, copy)   NSString  *classid;
@property (nonatomic, strong) NSMutableArray *classNames; //存放班级名称
@property (nonatomic, strong) NSMutableArray *classesArr; //存放班级
@property (nonatomic, strong) NSMutableArray *classesIDArr; //存放班级ID
@property (nonatomic,assign)  NSInteger indexOfType;
@property (nonatomic,assign)  NSInteger indexOfMonthType;//月
@property (nonatomic,assign)  NSInteger indexOfYearType;//年
@property (nonatomic, copy)   NSString *dateString;

@property (nonatomic,strong) UIView *KQJLView; //底部view

@property (nonatomic,strong) ParAttendLabel *amArrLabel; //上午到校的记录
@property (nonatomic,strong) ParAttendLabel *pmArrLabel; //下午到校的记录
@property (nonatomic,strong) ParAttendLabel *amLeaLabel; //上午离校的记录
@property (nonatomic,strong) ParAttendLabel *pmLeaLabel; //下午离校的记录
@property (nonatomic,strong) ParAttendLabel *qjLeaLabel; //请假的记录

@property (nonatomic,strong) ParAttendLabel *cqDaysLabel; //出勤天数

@property (nonatomic,strong) ParAttendLabel *qjDaysLabel; //请假天数

@property (nonatomic, strong) NSMutableArray *studentArr;
@property (nonatomic, strong) NSMutableArray *stuNameArr;
@property (nonatomic, strong) NSMutableArray *stuIDArr;

@property (nonatomic, strong) UITableView  *studentsTable;
@property (nonatomic, strong) UIView       *backView;


@end

@implementation FR_StudentAccountViewController

#pragma init

- (id)init
{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}

- (UIScrollView *)bgScrollView {
    
    if (_bgScrollView == nil) {
        _bgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGH-64)];
        [_bgScrollView setBackgroundColor:[UIColor whiteColor]];
        _bgScrollView.showsVerticalScrollIndicator = NO;
        _bgScrollView.showsHorizontalScrollIndicator = NO;
        
    }
    return _bgScrollView;
}
- (UIButton *)classBtn {
    
    if (_classBtn == nil) {
        _classBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_classBtn setFrame:CGRectMake(0, 12, 92, 26)];
        _classBtn.layer.borderWidth = 0.5;
        _classBtn.layer.cornerRadius = 4;
        _classBtn.layer.borderColor = [DEFAULTCOLOR CGColor];
        [_classBtn setBackgroundColor:colorFromHex(@"b1e7fc")];
        _classBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_classBtn setTitleColor:DEFAULTCOLOR forState:(UIControlStateNormal)];
        [_classBtn addTarget:self action:@selector(chooseClass) forControlEvents:(UIControlEventTouchUpInside)];
        
        
    }
    return _classBtn;
}

- (UIButton *)dateBtn {
    
    if (_dateBtn == nil) {
        _dateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_dateBtn setFrame:CGRectMake(0, 12, 92, 26)];
        _dateBtn.layer.borderWidth = 0.5;
        _dateBtn.layer.cornerRadius = 4;
        _dateBtn.layer.borderColor = [DEFAULTCOLOR CGColor];
        [_dateBtn setBackgroundColor:colorFromHex(@"b1e7fc")];
        [_dateBtn setTitleColor:DEFAULTCOLOR forState:(UIControlStateNormal)];
        _dateBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_dateBtn addTarget:self action:@selector(chooseDate) forControlEvents:(UIControlEventTouchUpInside)];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM"];
        
        NSString *dateStr = [formatter stringFromDate:[NSDate date]];
        [_dateBtn setTitle:dateStr forState:(UIControlStateNormal)];
        
    }
    return _dateBtn;
}

- (UIPickerView *)datePicker {
    
    if (_datePicker == nil) {
        _datePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, SCREENHEIGH - 170, SCREENWIDTH, 170)];
        _datePicker.delegate = self;
        _datePicker.dataSource = self;
        _datePicker.showsSelectionIndicator = NO;
        _datePicker.backgroundColor=[UIColor whiteColor];
        _datePicker.userInteractionEnabled=YES;
        [_datePicker selectRow:[thisMonth integerValue]-1 inComponent:1 animated:NO];
    }
    
    return _datePicker;
}

- (UIPickerView *)classPicker {
    
    if (_classPicker == nil) {
        _classPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, SCREENHEIGH - 170, SCREENWIDTH, 170)];
        _classPicker.delegate = self;
        _classPicker.dataSource = self;
        _classPicker.showsSelectionIndicator = NO;
        _classPicker.backgroundColor=[UIColor whiteColor];
        _classPicker.userInteractionEnabled=YES;

    }
    
    return _classPicker;
}

- (NSMutableArray *)classesArr{
    
    if (_classesArr == nil) {
        
        _classesArr = [NSMutableArray array];
        _classNames = [NSMutableArray array];
        _classesIDArr = [NSMutableArray array];
    }
    
    return _classesArr;
}

- (UIView *)backView {
    
    if (_backView == nil) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGH-64)];
        [_backView setBackgroundColor:[UIColor blackColor]];
        _backView.alpha = 0.5;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeClassTable)];
        [_backView addGestureRecognizer:tap];
    }
    return _backView;
}
- (UITableView *)studentsTable {
    
    if (_studentsTable == nil) {
        
        _studentsTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, 0) style:(UITableViewStylePlain)];
        _studentsTable.delegate = self;
        _studentsTable.dataSource = self;
        _studentsTable.rowHeight = 44;
        _studentsTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _studentsTable;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    NSDateFormatter *formatter1 =[[NSDateFormatter alloc]init];
    [formatter1 setDateFormat:@"yyyy-MM"];
    self.dateString = [formatter1 stringFromDate:[NSDate date]];
    
    lastMonth = self.dateString;//记录当前月份
    
    NSDateFormatter *formmater = [[NSDateFormatter alloc] init];
    [formmater setDateFormat:@"yyyy-MM-dd"];
    
    selDateStr = [formmater stringFromDate:[NSDate date]];
    
    thisYear = [[formmater stringFromDate:[NSDate date]] componentsSeparatedByString:@"-"][0];
    
    thisMonth = [[formmater stringFromDate:[NSDate date]] componentsSeparatedByString:@"-"][1];
    
    _indexOfMonthType = [thisMonth integerValue]-1;
    
    selectYearStr = [[formmater stringFromDate:[NSDate date]] componentsSeparatedByString:@"-"][0];
    
    [self setupUI];
    
    [self.view addSubview:self.bgScrollView];
 
    [self setTopView];
    
    [self getStudentListData];

    
    // Do any additional setup after loading the view.
}

#pragma View
- (void)setupUI {
    
    navBackView=[[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH/2-60, 20, 120, 44)];
    navBackView.userInteractionEnabled=YES;
    [self.view addSubview:navBackView];
    
    _navTitlelabel=[[UILabel alloc]initWithFrame:CGRectMake(0,10,120, 18)];
    _navTitlelabel.textAlignment = NSTextAlignmentCenter;
    _navTitlelabel.font = [UIFont boldSystemFontOfSize:18];
    _navTitlelabel.textColor = [UIColor blackColor];
    _navTitlelabel.userInteractionEnabled = YES;
    [navBackView addSubview:_navTitlelabel];
    
    _navImageView=[[UIImageView alloc]initWithFrame:CGRectMake(navBackView.frame.size.width/2-7.5,CGRectGetMaxY(_navTitlelabel.frame),15, 11)];
    _navImageView.userInteractionEnabled = YES;
    [navBackView addSubview:_navImageView];
    
    imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imgBtn.frame = CGRectMake(0, 0, 120, 44);
    imgBtn.hidden = YES;
    [imgBtn addTarget:self action:@selector(selStudent:) forControlEvents:(UIControlEventTouchUpInside)];
    [navBackView addSubview:imgBtn];
    
}

- (void)setTopView {
    
    UILabel *classLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 12, 62, 26)];
    classLabel.textColor = colorFromHex(@"#3b3b3b");
    classLabel.font = [UIFont systemFontOfSize:15];
    classLabel.text = @"选择班级";
    [self.bgScrollView addSubview:classLabel];
    
    self.classBtn.x = classLabel.right + 5;
    [self.bgScrollView addSubview:self.classBtn];
    
    self.dateBtn.x = SCREENWIDTH - 15 - 92;
    [self.bgScrollView addSubview:self.dateBtn];
    
    UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.dateBtn.x - 5 -62, 12, 62, 26)];
    dateLabel.textColor = colorFromHex(@"#3b3b3b");
    dateLabel.font = [UIFont systemFontOfSize:15];
    dateLabel.text = @"选择日期";
    [self.bgScrollView addSubview:dateLabel];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.dateBtn.bottom+12, SCREENWIDTH, 10)];
    [lineView setBackgroundColor:colorFromHex(@"#F5F8FA")];
    [self.bgScrollView addSubview:lineView];
    
    self.calender = [[FDCalendar alloc]initWithCurrentDate:[NSDate date]];
    self.calender.delegete = self;
    self.calender.today = [NSDate date];
    self.calender.frame = CGRectMake(0, lineView.bottom, self.view.frame.size.width, 352);
    [self.calender setValue:nil forKeyPath:@"centerCalendarItem.selectedIndexPath"];
    [self.bgScrollView addSubview:self.calender];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(15, self.calender.bottom, SCREENWIDTH, 0.5)];
    [line setBackgroundColor:colorFromHex(@"#DEDEDE")];
    [self.bgScrollView addSubview:line];
    
    UILabel * DRJLLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, line.bottom+14, SCREENWIDTH, 16)];
    DRJLLabel.text = @"当日考勤记录";
    DRJLLabel.textColor = DEFAULTCOLOR;
    DRJLLabel.font = [UIFont systemFontOfSize:16];
    [self.bgScrollView addSubview:DRJLLabel];
    
    line2 = [[UIView alloc]initWithFrame:CGRectMake(15, DRJLLabel.bottom+14, SCREENWIDTH, 0.5)];
    [line2 setBackgroundColor:colorFromHex(@"#DEDEDE")];
    [self.bgScrollView addSubview:line2];
    
    //当日考勤记录
    [self.bgScrollView addSubview:self.KQJLView];
    
    _bgScrollView.contentSize = CGSizeMake(SCREENWIDTH, self.KQJLView.frame.origin.y + self.KQJLView.size.height +20);
    
}
- (UIView *)KQJLView{
    
    if (!_KQJLView) {
        
        _KQJLView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(line2.frame), SCREENWIDTH, 306)];
        _KQJLView.backgroundColor = [UIColor whiteColor];
        
        QJLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 100, 16)];
        QJLabel.text = @"请假";
        QJLabel.font = [UIFont systemFontOfSize:15];
        QJLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        [_KQJLView addSubview:QJLabel];
        
        amLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 66, 100, 16)];
        amLabel.text = @"上午";
        amLabel.font = [UIFont systemFontOfSize:15];
        amLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        [_KQJLView addSubview:amLabel];
        
        pmLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 66+54, 100, 16)];
        pmLabel.text = @"下午";
        pmLabel.font = [UIFont systemFontOfSize:15];
        pmLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        [_KQJLView addSubview:pmLabel];
        
        _qjLeaLabel = [ParAttendLabel labelWithFrame:CGRectMake(15, CGRectGetMaxY(QJLabel.frame) + 12, (SCREENWIDTH - 30) / 2 , 16) desText:@"类型" conText:@"病假"];
        [_KQJLView addSubview:_qjLeaLabel];
        
        _amArrLabel = [ParAttendLabel labelWithFrame:CGRectMake(15, CGRectGetMaxY(amLabel.frame) + 12, (SCREENWIDTH - 30) / 2 , 16) desText:@"到校" conText:@"正常 07:30"];
        [_KQJLView addSubview:_amArrLabel];
        
        _amLeaLabel = [ParAttendLabel labelWithFrame:CGRectMake(15 + (SCREENWIDTH - 30) / 2, CGRectGetMaxY(amLabel.frame) + 12, (SCREENWIDTH - 30) / 2, 16) desText:@"离校" conText:@"未离校 12:00"];
        [_KQJLView addSubview:_amLeaLabel];
        
        _pmArrLabel = [ParAttendLabel labelWithFrame:CGRectMake(15, CGRectGetMaxY(pmLabel.frame) + 12, (SCREENWIDTH - 30) / 2, 16) desText:@"到校" conText:@"旷课"];
        [_KQJLView addSubview:_pmArrLabel];
        
        _pmLeaLabel = [ParAttendLabel labelWithFrame:CGRectMake(15 + (SCREENWIDTH - 30) / 2, CGRectGetMaxY(pmLabel.frame) + 12, (SCREENWIDTH - 30) / 2, 16) desText:@"离校" conText:@"暂无考勤记录"];
        [_KQJLView addSubview:_pmLeaLabel];
        
        line3 = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(_pmArrLabel.frame)+14, SCREENWIDTH, 0.5)];
        [line3 setBackgroundColor:colorFromHex(@"#DEDEDE")];
        [_KQJLView addSubview:line3];
        
        KQTJLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(line3.frame)+14, SCREENWIDTH, 16)];
        KQTJLabel.text = @"本月考勤统计";
        KQTJLabel.textColor = DEFAULTCOLOR;
        KQTJLabel.font = [UIFont systemFontOfSize:16];
        [_KQJLView addSubview:KQTJLabel];
        
        line4 = [[UIView alloc]initWithFrame:CGRectMake(15, KQTJLabel.bottom+14, SCREENWIDTH, 0.5)];
        [line4 setBackgroundColor:colorFromHex(@"#DEDEDE")];
        [_KQJLView addSubview:line4];
        
        _cqDaysLabel = [ParAttendLabel labelWithFrame:CGRectMake(15, CGRectGetMaxY(line4.frame) + 18, (SCREENWIDTH - 30) / 2, 16) desText:@"出勤天数" conText:@"12天"];
        [_KQJLView addSubview:_cqDaysLabel];
        
        _qjDaysLabel = [ParAttendLabel labelWithFrame:CGRectMake(15 + (SCREENWIDTH - 30) / 2, CGRectGetMaxY(line4.frame) + 18, (SCREENWIDTH - 30) / 2, 16) desText:@"请假天数" conText:@"3天"];
        [_KQJLView addSubview:_qjDaysLabel];

    }
    
    return _KQJLView;
}

//没有请假类型
- (void)setKQJLViewFrame
{
    
    _KQJLView.frame = CGRectMake(0, CGRectGetMaxY(line2.frame), SCREENWIDTH, 250);
    
    QJLabel.hidden = YES;
    
    _qjLeaLabel.hidden = YES;
    
    amLabel.frame = CGRectMake(15, 12, 100, 16);
    
    pmLabel.frame = CGRectMake(15, 12+54, 100, 16);
    
    _amArrLabel.frame = CGRectMake(15, CGRectGetMaxY(amLabel.frame) + 12, (SCREENWIDTH - 30) / 2 , 16);
    
    _amLeaLabel.frame = CGRectMake(15 + (SCREENWIDTH - 30) / 2, CGRectGetMaxY(amLabel.frame) + 12, (SCREENWIDTH - 30) / 2, 16);
    
    _pmArrLabel.frame = CGRectMake(15, CGRectGetMaxY(pmLabel.frame) + 12, (SCREENWIDTH - 30) / 2, 16);
    
    _pmLeaLabel.frame = CGRectMake(15 + (SCREENWIDTH - 30) / 2, CGRectGetMaxY(pmLabel.frame) + 12, (SCREENWIDTH - 30) / 2, 16);
    
    line3.frame = CGRectMake(15, CGRectGetMaxY(_pmArrLabel.frame)+14, SCREENWIDTH, 0.5);
    
    KQTJLabel.frame = CGRectMake(15, CGRectGetMaxY(line3.frame)+14, SCREENWIDTH, 16);
    
    line4.frame = CGRectMake(15, KQTJLabel.bottom+14, SCREENWIDTH, 0.5);
    
    _cqDaysLabel.frame = CGRectMake(15, CGRectGetMaxY(line4.frame) + 18, (SCREENWIDTH - 30) / 2, 16);
    
    _qjDaysLabel.frame = CGRectMake(15 + (SCREENWIDTH - 30) / 2, CGRectGetMaxY(line4.frame) + 18, (SCREENWIDTH - 30) / 2, 16);
    
    _bgScrollView.contentSize = CGSizeMake(SCREENWIDTH, self.KQJLView.frame.origin.y + self.KQJLView.size.height +20);
}
//有请假类型
- (void)setHaveQjViewFrame
{
    
    _KQJLView.frame = CGRectMake(0, CGRectGetMaxY(line2.frame), SCREENWIDTH, 306);
    
    QJLabel.hidden = NO;
    
    _qjLeaLabel.hidden = NO;
    
    QJLabel.frame = CGRectMake(15, 12, 100, 16);
    
    amLabel.frame = CGRectMake(15, 66, 100, 16);
    
    pmLabel.frame = CGRectMake(15, 66+54, 100, 16);
    
    _qjLeaLabel.frame = CGRectMake(15, CGRectGetMaxY(QJLabel.frame) + 12, (SCREENWIDTH - 30) / 2 , 16);
    
    _amArrLabel.frame = CGRectMake(15, CGRectGetMaxY(amLabel.frame) + 12, (SCREENWIDTH - 30) / 2 , 16);
    
    _amLeaLabel.frame = CGRectMake(15 + (SCREENWIDTH - 30) / 2, CGRectGetMaxY(amLabel.frame) + 12, (SCREENWIDTH - 30) / 2, 16);
    
    _pmArrLabel.frame = CGRectMake(15, CGRectGetMaxY(pmLabel.frame) + 12, (SCREENWIDTH - 30) / 2, 16);
    
    _pmLeaLabel.frame = CGRectMake(15 + (SCREENWIDTH - 30) / 2, CGRectGetMaxY(pmLabel.frame) + 12, (SCREENWIDTH - 30) / 2, 16);
    
    line3.frame = CGRectMake(15, CGRectGetMaxY(_pmArrLabel.frame)+14, SCREENWIDTH, 0.5);
    
    KQTJLabel.frame = CGRectMake(15, CGRectGetMaxY(line3.frame)+14, SCREENWIDTH, 16);
    
    line4.frame = CGRectMake(15, KQTJLabel.bottom+14, SCREENWIDTH, 0.5);
    
    _cqDaysLabel.frame = CGRectMake(15, CGRectGetMaxY(line4.frame) + 18, (SCREENWIDTH - 30) / 2, 16);
    
    _qjDaysLabel.frame = CGRectMake(15 + (SCREENWIDTH - 30) / 2, CGRectGetMaxY(line4.frame) + 18, (SCREENWIDTH - 30) / 2, 16);
    
    _bgScrollView.contentSize = CGSizeMake(SCREENWIDTH, self.KQJLView.frame.origin.y + self.KQJLView.size.height +20);
}


#pragma mark - Action
//展示班级
- (void)chooseClass {
    
    [control removeFromSuperview];
    control=[[UIControl alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH)];
    control.backgroundColor=[UIColor clearColor];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH-210)];
    [bgView setBackgroundColor:[UIColor clearColor]];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelControlAction)];
    [bgView addGestureRecognizer:tap];
    [control addSubview:bgView];
    
    
    
    UIView *sureBC = [[UIView alloc]initWithFrame:CGRectMake(0, SCREENHEIGH-210, SCREENWIDTH, 40)];
    [sureBC setBackgroundColor:[UIColor whiteColor]];
    [control addSubview:sureBC];
    
    UIButton *sureBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame=CGRectMake(SCREENWIDTH-12-40,SCREENHEIGH-210, 40, 40);
    [sureBtn setTitle:@"完成" forState:UIControlStateNormal];
    [sureBtn setTitleColor:RGBACOLOR(57, 173, 255, 1) forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(chooseTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    sureBtn.tag = 1;
    
    [control addSubview:sureBtn];
    
    if (self.classesArr) {
        
        NSArray * itemArrayindex = [self.classesArr objectAtIndex:index];
        
        [self.classNames removeAllObjects];
        [self.classesIDArr removeAllObjects];
        
        for (int i =0; i<itemArrayindex.count; i++) {
            
            NSDictionary *classDic = [itemArrayindex objectAtIndex:i];
            
            [self.classNames addObject:[classDic objectForKey:@"classname"]];
            [self.classesIDArr addObject:[classDic objectForKey:@"classid"]];
        }
        
        NSLog(@"self.classNames = %@  self.classesArr = %@",self.classNames,self.classesArr);
        
    }
    
    [control addSubview:self.classPicker];
    
    [_classPicker reloadAllComponents];//刷新班级picker数据
    
    UIWindow *window = KeyWindow;
    [window addSubview:control];
    
}

//展示日期
- (void)chooseDate {
    
    
    self.dateString = [NSString stringWithFormat:@"%@-01",thisYear];
    
    [control removeFromSuperview];
    control=[[UIControl alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH)];
    control.backgroundColor=[UIColor clearColor];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH-210)];
    [bgView setBackgroundColor:[UIColor clearColor]];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelControlAction)];
    [bgView addGestureRecognizer:tap];
    [control addSubview:bgView];
    
    UIView *sureBC = [[UIView alloc]initWithFrame:CGRectMake(0, SCREENHEIGH-210, SCREENWIDTH, 40)];
    [sureBC setBackgroundColor:[UIColor whiteColor]];
    [control addSubview:sureBC];
    
    UIButton *sureBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame=CGRectMake(SCREENWIDTH-12-40,SCREENHEIGH-210, 40, 40);
    [sureBtn setTitle:@"完成" forState:UIControlStateNormal];
    [sureBtn setTitleColor:RGBACOLOR(57, 173, 255, 1) forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(chooseTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    sureBtn.tag = 2;
    
    [control addSubview:sureBtn];
    
    [control addSubview:self.datePicker];
    
    UIWindow *window = KeyWindow;
    [window addSubview:control];
    
}

//选择班级和日期
- (void)chooseTypeAction:(UIButton *)sender
{
    [control removeFromSuperview];
    
    if (sender.tag == 1) {
        
        
        self.classid = [NSString stringWithFormat:@"%@",[self.classesIDArr objectAtIndex:_indexOfType]];
        
        [self.classBtn setTitle:[self.classNames objectAtIndex:_indexOfType] forState:(UIControlStateNormal)];
        
        [self loadRecordWithDate:self.dateString];
        
        [self requestRollCallStudentDateInfo:selDateStr];
        
    }else if (sender.tag == 2) {
        
        if ((_indexOfMonthType + 1) < 10) {
            
        self.dateString = [NSString stringWithFormat:@"%ld-0%ld",    [thisYear integerValue]-_indexOfYearType,_indexOfMonthType+1];
        }else
        {
        self.dateString = [NSString stringWithFormat:@"%ld-%ld",    [thisYear integerValue]-_indexOfYearType,_indexOfMonthType+1];
        }
        
        [self.dateBtn setTitle:self.dateString forState:(UIControlStateNormal)];
        
        [self loadRecordWithDate:self.dateString];
        
        if (![self.dateString isEqualToString:lastMonth]) {
            
            selDateStr = [NSString stringWithFormat:@"%@-01",self.dateString];
            
            [self requestRollCallStudentDateInfo:selDateStr];
            
            lastMonth = self.dateString;
            
        }
        
    }
}

//展示学生列表
- (void)selStudent:(UIButton *)sender {
    
    if (self.studentsTable.hidden == YES) {
        [self.studentsTable setHidden:NO];
        [self.backView setHidden:NO];
        [_navImageView setImage:[UIImage imageNamed:@"record_head_classchoose_up_"]];
    }else {
        [self.studentsTable setHidden:YES];
        [self.backView setHidden:YES];
        [_navImageView setImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
    }
    
}

- (void)removeClassTable {
    [self.studentsTable setHidden:YES];
    [self.backView setHidden:YES];
    [_navImageView setImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
}
- (void)cancelControlAction
{
    [control removeFromSuperview];
}

#pragma mark - PickerDelegate & PickerDataSource

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    if (pickerView == self.classPicker) {
        return 1;
    }
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (pickerView == self.classPicker) {
        
        NSLog(@"[self.classesArr objectAtIndex:index] count] = %lu",(unsigned long)[[self.classesArr objectAtIndex:index] count]);
        return [[self.classesArr objectAtIndex:index] count];
        
    }else if (pickerView == self.datePicker) {
        
        if (component == 0) {
            return 100;
        }else
        {
            return 12;
        }
    }
    
    return 0;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView == self.classPicker) {
        
        NSLog(@"self.classNames = %@",self.classNames);
        
        return [self.classNames objectAtIndex:row];
        
    }else if (pickerView == self.datePicker) {
        
        if (component==0) {
            
            return [NSString stringWithFormat:@"%ld年",[thisYear intValue]-row];
        }else {
            return [NSString stringWithFormat:@"%ld月",row+1];
            
        }
    }
    
    return nil;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (pickerView == self.classPicker) {
        
        self.indexOfType = row;
        
    }else if (pickerView == self.datePicker) {
        
        
        if (component == 0) {
            
            self.indexOfYearType = row;
            
        }else {
            
            self.indexOfMonthType = row;
        }
        
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    
    return 200;
}

#pragma mark = AlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - CalenderDelegate

-(void)didSelectedDate:(NSDate *)date
{
    NSDateFormatter *formatter1 =[[NSDateFormatter alloc]init];
    [formatter1 setDateFormat:@"yyyy-MM-dd"];
    
    NSTimeInterval  interval =-1;
    
    NSDate *seleceDate = [date initWithTimeInterval:interval sinceDate:date];
    
    selDateStr = [formatter1 stringFromDate:seleceDate];
    
    NSLog(@"选择的日期 == %@",selDateStr);
    
    [self requestRollCallStudentDateInfo: selDateStr];
    
}




#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.studentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"classIdentifier"];
    
    UILabel *className;
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"classIdentifier"];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 43.5, SCREENWIDTH, 0.5)];
        [lineView setBackgroundColor:colorFromHex(@"#DEDEDE")];
        [cell.contentView addSubview:lineView];
        
        className = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 44)];
        [className setFont:[UIFont systemFontOfSize:16]];
        [className setTextColor:colorFromHex(@"#3b3b3b")];
        className.textAlignment = NSTextAlignmentCenter;
        
        [cell.contentView addSubview:className];
        
    }
    
    className.text = [self.stuNameArr objectAtIndex:indexPath.row];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    index = indexPath.row;
    
    _indexOfType = 0;
    
    NSString *titleStr = [self.stuNameArr objectAtIndex:indexPath.row];
    
    
    selStudentID = [self.stuIDArr objectAtIndex:indexPath.row];
    
    _navTitlelabel.text = titleStr;
    [_navImageView setImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
    
    //选择默认班级
    if (self.classesArr) {
        NSArray * itemArrayindex = [self.classesArr objectAtIndex:indexPath.row];
        
        if (itemArrayindex) {
            
            NSDictionary *classDic0 = [itemArrayindex objectAtIndex:0];
            
            if (classDic0.count >0 ) {
                
                self.classid = [NSString stringWithFormat:@"%@",[classDic0 objectForKey:@"classid"]];
                [self.classBtn setTitle:[classDic0 objectForKey:@"classname"] forState:(UIControlStateNormal)];
            }
        }
        
    }
    
    
    [self.backView setHidden:YES];
    [self.studentsTable setHidden:YES];
    
    [self loadRecordWithDate:self.dateString];
    
    [self requestRollCallStudentDateInfo:selDateStr];
}

#pragma mark - Request

//获取学生列表
- (void)getStudentListData {
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:USERID forKey:@"parentid"];
    
    [Utils Java_get:@"api/getChildClass" parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dict = (NSDictionary *)jsonObject;
        if ([[dict objectForKey:@"re_code"] integerValue]==0) {
            if ([[dict objectForKey:@"re_data"] count]>0) {
                
                NSLog(@"dict = %@",dict);
                
                self.studentArr = [dict objectForKey:@"re_data"];
                
                self.stuNameArr = [[NSMutableArray alloc]init];
                
                self.stuIDArr = [[NSMutableArray alloc]init];
                
                for (NSDictionary *dic in self.studentArr) {
                    
                    [self.stuNameArr addObject:[dic valueForKey:@"studentname"]];
                    [self.stuIDArr addObject:[dic valueForKey:@"studentid"]];
                    
                    [self.classesArr addObject:[dic valueForKey:@"item"]];
                    
                }
                
                NSLog(@"11111%@ %@ %@",self.stuNameArr,self.stuIDArr,self.classesArr);
                
                if (self.studentArr.count > 1) {
                    
                    [_navImageView setImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
                    
                    [navBackView addSubview:_navImageView];
                    
                    self->imgBtn.hidden = NO;
                }
                
                
                NSString *titleStr = [self.stuNameArr objectAtIndex:0];
                selStudentID = [self.stuIDArr objectAtIndex:0];
                _navTitlelabel.text = titleStr;
                
                //选择默认班级
                if (self.classesArr.count >0) {
                    NSArray * itemArray0 = [self.classesArr objectAtIndex:0];
                    
                    if (itemArray0.count>0) {
                        
                        NSDictionary *classDic0 = [itemArray0 objectAtIndex:0];
                        
                        if (classDic0.count >0 ) {
                            
                            self.classid = [NSString stringWithFormat:@"%@",[classDic0 objectForKey:@"classid"]];
                            [self.classBtn setTitle:[classDic0 objectForKey:@"classname"] forState:(UIControlStateNormal)];
                        }
                    }
                    
                }
                
                [self loadRecordWithDate:self.dateString];
                
                [self requestRollCallStudentDateInfo:selDateStr];
                
                
                
                [self.view addSubview:self.backView];
                
                [self.view addSubview:self.studentsTable];
                
                if (44*self.stuNameArr.count > SCREENHEIGH - 64 - 44) {
                    self.studentsTable.scrollEnabled = YES;
                    [self.studentsTable setHeight:SCREENHEIGH - 64 - 44];
                }else {
                    
                    [self.studentsTable setHeight:44 * self.stuNameArr.count];
                    self.studentsTable.scrollEnabled = NO;
                }
                
                [self.studentsTable setHidden:YES];
                
                [self.backView setHidden:YES];
                
            }
            
        }
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
    /*
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_leave_getstudentandclass" forKey:@"method"];
    [dict setObject:USERID forKey:@"parentid"];
    [Utils get:nil parameters:dict success:^(NSInteger rocode, id resposeJson){
        NSDictionary * dict = (NSDictionary *)resposeJson;
        if ([[dict objectForKey:@"re_code"] integerValue]==0) {
            if ([[dict objectForKey:@"re_data"] count]>0) {
                
                NSLog(@"dict = %@",dict);
                
                self.studentArr = [dict objectForKey:@"re_data"];
                
                self.stuNameArr = [[NSMutableArray alloc]init];
                
                self.stuIDArr = [[NSMutableArray alloc]init];
                
                for (NSDictionary *dic in self.studentArr) {
                    
                    [self.stuNameArr addObject:[dic valueForKey:@"studentname"]];
                    [self.stuIDArr addObject:[dic valueForKey:@"studentid"]];
                    
                    [self.classesArr addObject:[dic valueForKey:@"item"]];
                    
                }
                
                NSLog(@"11111%@ %@ %@",self.stuNameArr,self.stuIDArr,self.classesArr);

                if (self.studentArr.count > 1) {
                    
                    [_navImageView setImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
                    
                    [navBackView addSubview:_navImageView];
                    
                    self->imgBtn.hidden = NO;
                }
                
                
                NSString *titleStr = [self.stuNameArr objectAtIndex:0];
                selStudentID = [self.stuIDArr objectAtIndex:0];
                _navTitlelabel.text = titleStr;
                
                //选择默认班级
                if (self.classesArr.count >0) {
                    NSArray * itemArray0 = [self.classesArr objectAtIndex:0];
                    
                    if (itemArray0.count>0) {
                        
                        NSDictionary *classDic0 = [itemArray0 objectAtIndex:0];
                        
                        if (classDic0.count >0 ) {
                            
                           self.classid = [NSString stringWithFormat:@"%@",[classDic0 objectForKey:@"classid"]];
                            [self.classBtn setTitle:[classDic0 objectForKey:@"classname"] forState:(UIControlStateNormal)];
                        }
                    }
                    
                }
                
                [self loadRecordWithDate:self.dateString];
                
                [self requestRollCallStudentDateInfo:selDateStr];

                
                
                [self.view addSubview:self.backView];
                
                [self.view addSubview:self.studentsTable];
                
                if (44*self.stuNameArr.count > SCREENHEIGH - 64 - 44) {
                    self.studentsTable.scrollEnabled = YES;
                    [self.studentsTable setHeight:SCREENHEIGH - 64 - 44];
                }else {
                    
                    [self.studentsTable setHeight:44 * self.stuNameArr.count];
                    self.studentsTable.scrollEnabled = NO;
                }
                
                [self.studentsTable setHidden:YES];
                
                [self.backView setHidden:YES];
                
            }

        }
        
    } failure:^(NSInteger rocode, NSError * error){
        
    }];
     */
}


//获取日历数据
- (void)loadRecordWithDate:(NSString *)monDate {
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:self.classid forKey:@"classid"];
    [dict setObject:monDate forKey:@"date"];
    [dict setObject:selStudentID forKey:@"studentid"];
    [dict setObject:SCHOOLID forKey:@"scid"];
    
    
    [Utils Java_get:@"api/getStuCalendar" parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary *dic = (NSDictionary *)jsonObject;
        
        NSDateFormatter *formatter1 =[[NSDateFormatter alloc]init];
        [formatter1 setDateFormat:@"yyyy-MM"];
        
        self.calender.markArr = [[dic objectForKey:@"re_data"] objectForKey:@"array"];
        
        self.calender.date = [formatter1 dateFromString:self.dateString];
        
        NSString *monthabo = [[dic objectForKey:@"re_data"] objectForKey:@"monthabo"];
        NSString *monthleave = [[dic objectForKey:@"re_data"] objectForKey:@"monthleave"];
        
        [self->_cqDaysLabel setContentText:[NSString stringWithFormat:@"%@天",monthabo]];
        [self->_qjDaysLabel setContentText:[NSString stringWithFormat:@"%@天",monthleave]];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
    /*
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_face_studentcalendar" forKey:@"method"];
    [dict setObject:self.classid forKey:@"classid"];
    [dict setObject:monDate forKey:@"date"];
    [dict setObject:selStudentID forKey:@"studentid"];

    NSLog(@"keys=%@",dict);
    [Utils get:nil parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary *dic = (NSDictionary *)jsonObject;
        
        NSLog(@"dict=%@",dic);
        NSDateFormatter *formatter1 =[[NSDateFormatter alloc]init];
        [formatter1 setDateFormat:@"yyyy-MM"];
        
        self.calender.markArr = [dic objectForKey:@"re_data"];
        
        self.calender.date = [formatter1 dateFromString:self.dateString];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
     */
}

//某日某班某学生考勤详情
- (void)requestRollCallStudentDateInfo:(NSString *)dataString
{
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:self.classid forKey:@"classid"];
    [dict setObject:selDateStr forKey:@"datetime"];
    [dict setObject:[NSString stringWithFormat:@"%@",selStudentID] forKey:@"studentid"];
    [dict setObject:SCHOOLID forKey:@"scid"];
    
    [Utils Java_get:@"api/getStuCalendarDetail" parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary *dict = [jsonObject objectForKey:@"re_data"];
        
        NSString *leaveinfo = [dict objectForKey:@"leaveinfo"];
        NSString *afternoonarrayStr = [dict objectForKey:@"afternoonarray"];
        NSString *afternoonleave = [dict objectForKey:@"afternoonleave"];
        NSString *beforenoonarray = [dict objectForKey:@"beforenoonarray"];
        NSString *beforenoonleave = [dict objectForKey:@"beforenoonleave"];
        
        
        
        if (leaveinfo.length == 0) {
            
            [self setKQJLViewFrame];
        }else
        {
            [self setHaveQjViewFrame];
            
            [self->_qjLeaLabel setContentText:leaveinfo];
        }
        
        [self->_amArrLabel setContentText:beforenoonarray];
        [self->_amLeaLabel setContentText:beforenoonleave];
        [self->_pmArrLabel setContentText:afternoonarrayStr];
        [self->_pmLeaLabel setContentText:afternoonleave];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
    
    /*
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_face_rollcallstudentdateinfo" forKey:@"method"];
    [dict setObject:self.classid forKey:@"classid"];
    [dict setObject:selDateStr forKey:@"datetime"];
    [dict setObject:selStudentID forKey:@"studentid"];
    
    NSLog(@"keysssss=%@",dict);
    
    [Utils get:nil parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        
        NSDictionary *dict = [jsonObject objectForKey:@"re_data"];
        NSLog(@"dictttttt=%@",dict);
        
        NSString *leaveinfo = [dict objectForKey:@"leaveinfo"];
        NSString *afternoonarrayStr = [dict objectForKey:@"afternoonarray"];
        NSString *afternoonleave = [dict objectForKey:@"afternoonleave"];
        NSString *beforenoonarray = [dict objectForKey:@"beforenoonarray"];
        NSString *beforenoonleave = [dict objectForKey:@"beforenoonleave"];
        NSString *monthabo = [dict objectForKey:@"monthabo"];
        NSString *monthleave = [dict objectForKey:@"monthleave"];
        
        NSLog(@"dict= %@",dict);
        
        if (leaveinfo.length == 0) {
            
            [self setKQJLViewFrame];
        }else
        {
            [self setHaveQjViewFrame];
            
            [self->_qjLeaLabel setContentText:leaveinfo];
        }
        
        [self->_amArrLabel setContentText:beforenoonarray];
        [self->_amLeaLabel setContentText:beforenoonleave];
        [self->_pmArrLabel setContentText:afternoonarrayStr];
        [self->_pmLeaLabel setContentText:afternoonleave];
        [self->_cqDaysLabel setContentText:[NSString stringWithFormat:@"%@天",monthabo]];
        [self->_qjDaysLabel setContentText:[NSString stringWithFormat:@"%@天",monthleave]];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
     */
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
